home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / FileLib / FileInfoLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-08  |  3.7 KB  |  141 lines  |  [TEXT/KAHL]

  1. /* Functions for getting Finder information about files and directories.
  2.  
  3.     93/10/13 AIH
  4.     - added FileIsFolder
  5.     - removed unnecessary functions
  6.     - added functions to get a file's creator and type
  7.     
  8.     91/05/27 AIH
  9.     - added a few comments
  10.     
  11.     91/05/06 AIH
  12.     - Wrote functions for getting and setting all of a file's information
  13.     - Fixed bug when setting the extra Finder information
  14.     
  15.     91/04/21 AIH
  16.     - Function for testing if a file exists returns an error code if the
  17.     file doesn't exist, instead of setting a flag. */
  18.     
  19. #include <limits.h>
  20. #include <string.h>
  21. #include "pstr.h"
  22. #include "MacLib.h"
  23. #include "MemoryLib.h"
  24. #include "StringLib.h"
  25. #include "FileLib.h"
  26.  
  27. /*----------------------------------------------------------------------------*/
  28. /* getting catalog information about files and directories */
  29. /*----------------------------------------------------------------------------*/
  30.  
  31. /* get information for file */
  32. void FileInfoGet(FileType *fp, FInfo *info)
  33. {
  34.     require(FileValid(fp));
  35.     FailOSErr(HGetFInfo(fp->vol, fp->dir, fp->pnm, info));
  36. }
  37.  
  38. /* get information for file */
  39. void FileInfoSet(FileType *fp, FInfo *info)
  40. {
  41.     require(FileValid(fp));
  42.     FailOSErr(HSetFInfo(fp->vol, fp->dir, fp->pnm, info));
  43. }
  44.  
  45. /* return the file's creator */
  46. OSType FileCreator(FileType *fp)
  47. {
  48.     FInfo info;
  49.     FileInfoGet(fp, &info);
  50.     return(info.fdCreator);
  51. }
  52.  
  53. /* return the file's type */
  54. OSType FileTypeGet(FileType *fp)
  55. {
  56.     FInfo info;
  57.     FileInfoGet(fp, &info);
  58.     return(info.fdType);
  59. }
  60.  
  61. /* get the file's catalog */
  62. void FileCatalog(FileType *fp, CInfoPBRec *pb)
  63. {
  64.     require(FileValid(fp));    
  65.     memclr(pb, sizeof(CInfoPBRec));
  66.     if (*fp->pnm)
  67.         pb->hFileInfo.ioNamePtr = fp->pnm;
  68.     pb->hFileInfo.ioVRefNum = fp->vol;
  69.     pb->hFileInfo.ioDirID = fp->dir;
  70.     FailOSErr(PBGetCatInfo(pb, false));
  71. }
  72.  
  73. /* set the file's catalog */
  74. void FileCatalogSet(FileType *fp, CInfoPBRec *pb)
  75. {
  76.     require(FileValid(fp));    
  77.     pb->hFileInfo.ioNamePtr = fp->pnm;
  78.     pb->hFileInfo.ioVRefNum = fp->vol;
  79.     pb->hFileInfo.ioDirID = fp->dir;
  80.     FailOSErr(PBSetCatInfo(pb, false));
  81. }
  82.  
  83. /*----------------------------------------------------------------------------*/
  84. /* getting other information about files and directories */
  85. /*----------------------------------------------------------------------------*/
  86.  
  87. /* Given a file structure specifying a directory, sets the file's
  88.     directory ID to the ID of the named directory and clears
  89.     the file's name:
  90.         Struct    Input                                 Output
  91.         fp->vol    Volume reference number --> Volume reference number
  92.         fp->dir    Parent directory ID        --> Directory ID
  93.         fp->cnm    Name of directory            --> (null)
  94.         fp->pnm    Name of directory            --> (null)
  95.     This function is most useful for moving into the offspring of a
  96.     directory without using partial pathnames: simply follow the call
  97.     to this function with a call to FileNameSet and then to FileResolve. */
  98. void FileDirID(FileType *fp)
  99. {
  100.     CInfoPBRec pb;
  101.     
  102.     require(FileValid(fp));
  103.     FileCatalog(fp, &pb);
  104.     if ((pb.dirInfo.ioFlAttrib & (1 << kFolderBit)) == 0)
  105.         FailOSErr(fnfErr); /* not a directory */
  106.     fp->dir = pb.dirInfo.ioDrDirID;
  107.     *fp->cnm = *fp->pnm = 0;
  108. }
  109.  
  110. /* Return true a is the same as b. Notice that two files are only
  111.     considered identical if they are on the same volume or in the same
  112.     working directory, in addition to having identical names and
  113.     directory IDs. */
  114. Boolean FilesAreSame(FileType *a, FileType *b)
  115. {
  116.     require(FileValid(a));
  117.     require(FileValid(b));    
  118.     return(a->vol == b->vol && a->dir == b->dir &&
  119.              EqualString(a->pnm, b->pnm, false, true));
  120. }
  121.  
  122. /* true if the file exists */
  123. Boolean FileExists(FileType *fp)
  124. {
  125.     CInfoPBRec pb;
  126.     volatile Boolean exists = true;
  127.     
  128.     TRY {
  129.         if (exists) {
  130.             FileCatalog(fp, &pb);
  131.             exists = ((pb.hFileInfo.ioFlAttrib & (1 << kFolderBit)) == 0);
  132.         }
  133.     } CATCH {
  134.         if (FailReason() == fnfErr) {
  135.             exists = false;
  136.             RETRY;
  137.         }
  138.     } ENDTRY;
  139.     return(exists);
  140. }
  141.